/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-13 17:27:49
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-19 16:48:59
 * @Description: 账单控制层
 */
const BillService = require("../service/bill.service");
const UserService = require("../service/user.service");
const { systemError, updateError } = require("../constant/index.js");
const { DEFAULT_PAGESIZE } = require("../config/config.default");

class BillController {
  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 新增账单
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async add(ctx, next) {
    try {
      const { name, zodiac } = ctx.request.body;
      const uid = ctx.state.userInfo.uid;
      let res = await BillService.add({ name, zodiac, uid });
      ctx.body = {
        code: "00000",
        msg: "请求成功",
        data: res,
      };
    } catch (err) {
      console.log(err);
      systemError.data = err.toString();
      ctx.app.emit("error", systemError, ctx);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 删除账单
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async delete(ctx, next) {
    try {
      const { bids } = ctx.request.body;
      let lastOpt = bids;
      if (typeof bids === "string") {
        lastOpt = [bids];
      }
      let res = await BillService.delete(lastOpt);
      ctx.body = {
        code: "00000",
        msg: "请求成功",
        data: res,
      };
    } catch (err) {
      console.log(err);
      systemError.data = err.toString();
      ctx.app.emit("error", systemError, ctx);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 获取账单列表
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async list(ctx, next) {
    try {
      const option = ctx.request.query;
      let lastOpt = { pageNo: 1, pageSize: Number(DEFAULT_PAGESIZE) };
      if (option && Object.keys(option).length > 0) {
        option.pageNo &&
          Object.assign(lastOpt, { pageNo: Number(option.pageNo) });
        option.pageSize &&
          Object.assign(lastOpt, { pageSize: Number(option.pageSize) });
        option.name && Object.assign(lastOpt, { name: option.name });
        option.zodiac && Object.assign(lastOpt, { zodiac: option.zodiac });
        option.dateS && Object.assign(lastOpt, { dateS: option.dateS });
        option.dateE && Object.assign(lastOpt, { dateE: option.dateE });
      }
      let res = await BillService.list(lastOpt, ctx.state.userInfo.uid);
      ctx.body = {
        code: "00000",
        msg: "请求成功",
        data: res,
      };
    } catch (err) {
      console.log(err);
      systemError.data = err.toString();
      ctx.app.emit("error", systemError, ctx);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 获取账单详情
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async detail(ctx, next) {
    try {
      const { bid } = ctx.request.query;
      let res = await BillService.detail(bid);
      let creater = await UserService.getUserInfo(res.uid, ["name"]);
      ctx.body = {
        code: "00000",
        msg: "请求成功",
        data: { ...res, creater: creater.name },
      };
    } catch (err) {
      console.log(err);
      systemError.data = err.toString();
      ctx.app.emit("error", systemError, ctx);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 更新账单
   * @param {*} ctx
   * @param {*} next
   * @return {*}
   */
  static async update(ctx, next) {
    try {
      const { bid, name, sum_income, sum_expenses, zodiac, actors } =
        ctx.request.body;
      const res = await BillService.update(bid, {
        name,
        sum_income,
        sum_expenses,
        zodiac,
        actors,
      });
      if (res > 0) {
        ctx.body = {
          code: "00000",
          msg: "更新成功",
          data: null,
        };
      } else {
        ctx.app.emit("error", updateError, ctx);
      }
    } catch (err) {
      console.log(err);
      systemError.data = err.toString();
      ctx.app.emit("error", systemError, ctx);
    }
  }
}

module.exports = BillController;
